<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="商品名称" prop="name">
                    <el-input
                        v-model="queryParams.name"
                        placeholder="请输入商品名称"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" shadow="never">
            <div>
                <el-button v-perms="['shop:add']" type="primary" @click="handleAdd">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="商品ID" prop="id" min-width="80" />
                <el-table-column label="商品名称" prop="title" min-width="150" show-overflow-tooltip />
                <el-table-column label="商品分类" min-width="120">
                    <template #default="{ row }">
                        {{ shopTypeList.find(item => item.id == row.cid)?.name || '未知' }}
                    </template>
                </el-table-column>
                <el-table-column label="商品封面图" prop="image" min-width="120">
                    <template #default="{ row }">
                        <el-image
                            v-if="row.image"
                            class="w-[50px] h-[50px] rounded"
                            :src="jsonToArr(row.image)[0]"
                            :preview-src-list="jsonToArr(row.image)"
                            fit="cover"
                            :preview-teleported="true"
                        />
                        <span v-else class="text-tx-secondary">暂无图片</span>
                    </template>
                </el-table-column>
                <el-table-column label="商品价格" prop="price" min-width="100">
                    <template #default="{ row }">
                        <span class="text-primary font-medium">¥{{ row.price }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="库存" prop="stock" min-width="180" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['shop:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['shop:delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>

        <!-- 编辑弹窗 -->
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup>
import { apiShopLists, apiShopDelete, apiShopCategoryLists } from '@/api/shop'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

defineOptions({
    name: 'ShopLists'
})

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)

// 查询条件
const queryParams = reactive({
    name: '',
    type: ''
})


// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiShopLists,
    params: queryParams
})


// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除该商品吗？')
    await apiShopDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

// 商品类型列表
const shopTypeList = ref<any[]>([])

// 获取商品类型列表
const getShopTypeList = async () => {
    try {
        const data = await apiShopCategoryLists({
            page: 1,
            limit: 999
        })
        shopTypeList.value = data.lists
    } catch (error) {
        console.error('获取商品类型失败:', error)
    }
}
// JSON字符串转数组
const jsonToArr = (jsonStr: string) => {
    try {
        return jsonStr.split(',')
        // return JSON.parse(jsonStr)
    } catch (error) {
        return []
    }
}
// 初始化
onMounted(() => {
    getLists()
    getShopTypeList()
})
</script>

<style scoped>

</style>